package com.example.shirotest.quartz;

import com.example.shirotest.console.Maingeter;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class QuartzStart implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        clearQueue();

        getData();
    }

    //定时清空去重数据
    public void clearQueue() throws Exception {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail job = JobBuilder.newJob(TestJob.class).build();
        //每天晚上23执行
        Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0 0 23 * * ?")).build();

        scheduler.scheduleJob(job,trigger);

        scheduler.start();
    }


    public void getData() throws Exception{
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail job = JobBuilder.newJob(GetDataJob.class).build();
        //每天4,6,8,10,12,14,16,18,20,22点的第十分钟执行
        Trigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0 10 4,6,8,10,12,14,16,18,20,22 * * ?")).build();

        scheduler.scheduleJob(job,trigger);

        scheduler.start();
    }

}
